package com.sciencep.pro.admin.system.controller;

import com.sciencep.pro.admin.system.common.util.UserUtil;
import com.sciencep.pro.common.domain.ResponseResult;
import com.sciencep.pro.common.exception.CustomException;
import com.sciencep.pro.core.domain.dto.power.PowerTO;
import com.sciencep.pro.core.domain.dto.user.UserDetail;
import com.sciencep.pro.core.service.IPowerService;
import com.sciencep.pro.core.service.IRolePowerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/power")
public class PowerController {

    @Autowired
    private IPowerService powerService;

    @Autowired
    private IRolePowerService rolePowerService;

    @Autowired
    private UserUtil userUtil;

    @PostMapping(value = "/list")
    @ApiOperation(value = "权限管理", notes = "角色权限展示")
    public ResponseResult<List<PowerTO>> list(String roleId) {
        List<PowerTO> resultList = powerService.getList(roleId);
        return new ResponseResult<List<PowerTO>>().success(resultList);
    }

    @PostMapping(value = "/save")
    public ResponseResult<Object> save(String roleId,@RequestParam List<String> powerIds) throws CustomException {
        UserDetail detail = userUtil.getCurrentUserDetail();
        if (rolePowerService.save(roleId, powerIds, detail)) {
            return new ResponseResult<>().success();
        } else {
            return new ResponseResult<>().failure();
        }
    }

}
